/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.trade.handler;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.imoney.framework.base.entity.IRequest;
import com.imoney.framework.base.entity.Response;
import com.imoney.framework.base.exception.ServiceException;
import com.imoney.framework.base.utils.StringUtils;
import com.imoney.payment.chain.HandlerChain;
import com.imoney.payment.chain.IHandler;
import com.imoney.payment.trade.common.InfoCode;
import com.imoney.payment.trade.domain.PayRequest;
import com.imoney.payment.trade.domain.PaymentItem;
import com.imoney.payment.trade.domain.TradeOrder;
import com.imoney.payment.trade.service.ITradeQueryService;

/**
 * 类说明：检查参数<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-6-6		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-6-6
 */
@Service
public class PayParamHandler implements IHandler<List<TradeOrder>> {

	@Resource
	private ITradeQueryService tradeQueryService;

	@Override
	public void doHandler(HandlerChain<List<TradeOrder>> chain,
			IRequest request, Response<List<TradeOrder>> response)
			throws ServiceException {
		PayRequest payRequest = (PayRequest) request;
		response.setCode(InfoCode.PARAM_IS_NULL);
		List<String> tradeNoList = payRequest.getTradeNoList();
		if (null == tradeNoList || tradeNoList.isEmpty()) {
			response.setMessage("trade no is null!");
			return;
		}
		if (StringUtils.isEmpty(payRequest.getBuyerEmail())) {
			response.setMessage("buyer is null!");
			return;
		}
		if (payRequest.isCheckPwd()
				&& StringUtils.isEmpty(payRequest.getPayPwd())) {
			response.setMessage("pay password is null!");
			return;
		}

		List<PaymentItem> paymentList = payRequest.getPaymentList();
		for (PaymentItem item : paymentList) {
			if (null == item.getBuyerPayType()) {
				response.setMessage("buyer pay type is null!");
				return;
			}
			if (StringUtils.isEmpty(item.getBuyerActNo())) {
				response.setMessage("buyer account no is null!");
				return;
			}
			if (null == item.getSellerPayType()) {
				response.setMessage("seller pay type is null!");
				return;
			}
			if (StringUtils.isEmpty(item.getSellerActNo())) {
				response.setMessage("seller account no is null!");
				return;
			}
			if (item.getAmt() <= 0) {
				response.setMessage("amount is 0!");
				return;
			}
		}

		List<TradeOrder> tradeList = tradeQueryService.queryTradeList(
				payRequest.getBuyerEmail(), payRequest.getTradeNoList());
		if (null == tradeList || tradeList.isEmpty()
				|| tradeList.size() < payRequest.getTradeNoList().size()) {
			response.setCode(InfoCode.PAYORDER_ISNOT_EXIST);
			response.setMessage("trade order is not exist");
			return;
		}

		response.setData(tradeList);
		response.setCode(InfoCode.SUCCEED);
		/** 处理下一个任务 */
		chain.doHandler(chain, request, response);
		if (InfoCode.SUCCEED == response.getCode()) {
			response.setMessage("transfer succeed!");
		}
	}

}
